package GtEngine

import (
	// "fmt"
	"strconv"
	"sync"
	"time"
)

import (
	. "tksConfig"
	// . "tksLog"
	. "GtDB"
	"tksDB"
	. "tksStatic"
)

var GateInfo *GateServerInfo
var Gatemutex *sync.Mutex
var t *time.Ticker

func GateRun() {
	Gatemutex = new(sync.Mutex)
	t = time.NewTicker(3 * time.Second)
	//
	GateInfo = new(GateServerInfo)
	id, _ := Config.Get("server").Get("id").Int()
	GateInfo.GateId = uint32(id)
	host, _ := Config.Get("server").Get("ip").String()
	port, _ := Config.Get("server").Get("port").Int()
	GateInfo.Addr = host + ":" + strconv.Itoa(port)
	GateInfo.ClientCount = 0
	go func() {
		for {
			select {
			case <-t.C:
				//定时更新自己的信息到缓存中
				go func() {
					Clientsmutex.Lock()
					defer Clientsmutex.Unlock()
					GateInfo.Ts = time.Now().Unix()
					tksDB.UpdateGateInfo(&Cache.Redis, GateInfo)
				}()
			}
		}

	}()
}

func GateAddClient() {
	Clientsmutex.Lock()
	defer Clientsmutex.Unlock()
	GateInfo.ClientCount++
}

func GateSubClient() {
	Clientsmutex.Lock()
	defer Clientsmutex.Unlock()
	GateInfo.ClientCount--
}
